# from users.models import EmailVerifyCode
# from random import choice
from random import randrange
from django.core.mail import send_mail

from django.conf import settings

# def get_random_code(code_length):
#     code_source = '1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'
#     code = ''
#     for i in range(code_length):
#         #随机选择一个字符
#         str_code = code_source[randrange(0, len(code_source)-1)]
#         code += str_code
#         # code += choice(code_source)
#     return code


def send_email_code(email, code, send_type=1):
    #第一步，创建邮箱验证码对象，保存数据库，用来以后做对比
    # code = get_random_code(8)
    # a = EmailVerifyCode()
    # a.email = email
    # a.send_type = send_type
    # a.code = code
    # a.save()

    #第二步: 正式的发邮件功能

    send_title = ''
    send_body = ''

    if send_type == 1:
        send_title = '欢迎注册袋鼠二手书网站'
        send_body = '您的激活码是：' + code
        send_mail(send_title, send_body, settings.EMAIL_FROM, [email])

    if send_type == 2:
        send_title = '谷粒教育重置密码系统'
        send_body = '请点击以下链接重置您的密码：\n http://127.0.0.1:8000/users/user_reset/' + code
        send_mail(send_title, send_body, settings.EMAIL_FROM, [email])

